<template>
    <div>
        <j-modal
        class="xshistory"
        title="分配成果"
        :visible="visible"
        :width="1000"
        :maskClosable="false"
        :switchFullscreen="switchFullscreen"
        @cancel="handleCancel"
        @ok="handleOk"
        >
            <div>
                <div style="margin-bottom: 16px;">
                    <a-button type="primary" style="margin-right:24px;" @click="deleteResult">删除</a-button>
                    <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选成果: 已选中<a style="font-weight: 600">{{ selectedRowKeysArr.length }}</a>个成果，共<a style="font-weight: 600">{{selectTotal}}</a>个成果
                    <!-- <a style="margin-left: 24px" @click="onClearSelected">清空</a> -->
                </div>
                <!-- :pagination="ipagination" @change="handleTableChange"-->
                <a-table
                    ref="table"
                    size="middle"
                    :scroll="{x: true}"
                    bordered
                    rowKey="id1"
                    :columns="selectColumns"
                    :dataSource="dataSource"
                    :loading="loading"
                    class="j-table-force-nowrap"
                    :rowSelection="{selectedRowKeys: selectedRowKeysArr, onChange: onSelectChangeArr}"
                >
                    <span slot="cxj_xmbh" slot-scope="text, record">
                        <a @click="viewDetail(record)">
                            {{record.cxj_xmbh}}
                        </a>
                    </span>
                    <!-- 答辩时间 -->
                    <!-- <span slot="cxj_dbsj" slot-scope="text, record">
                        <span style="margin-right:10px;">{{record.dbsj}}</span>
                        <a>
                            <img @click="handleUpdate(1, record.fzxmid)" style="width:14px;height:14px;cursor:pointer;" src="../../../assets/img/xuanze.png" alt="">
                        </a>
                    </span> -->
                    <!-- 答辩序号 -->
                    <span slot="cxj_xmxh" slot-scope="text, record">
                        <span style="margin-right:10px;">{{record.xm_xh}}</span>
                        <!-- <a>
                            <img @click="handleUpdate(2, record.fzxmid)" style="width:14px;height:14px;cursor:pointer;" src="../../../assets/img/xuanze.png" alt="">
                        </a> -->
                    </span>
                    <!-- 主审专家 -->
                    <!-- <span slot="cxj_zjxm" slot-scope="text, record">
                        <span style="margin-right:10px;">{{record.zj_xm}}</span>
                        <a>
                            <img @click="handleUpdate(3, record.fzxmid)" style="width:14px;height:14px;cursor:pointer;" src="../../../assets/img/xuanze.png" alt="">
                        </a>
                    </span> -->
                </a-table>
            </div>
            <div class="table-page-search-wrapper" style="margin-top:16px;">
                <!--  @keyup.enter.native="searchQuery" -->
                <a-form layout="inline">
                    <a-row :gutter="24">
                        <a-col :span="8">
                            <a-form-item label="成果编号或名称">
                                <a-input placeholder="请填写成果编号或名称" v-model="queryParam.gjc"></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :span="8">
                            <a-form-item label="申报单位">
                                <a-input placeholder="请填写申报单位" v-model="queryParam.sbdwmc"></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :span="8">
                            <a-form-item label="成果类别">
                                <a-select @change="handlCglb" v-model="queryParam.flid" placeholder="请选择成果类别">
                                    <a-select-option v-for="(item, index) in cxjSbList" :key="index" :value="item.fl_id">{{item.fl_mc}}</a-select-option>
                                </a-select>
                            </a-form-item>
                        </a-col>
                        <!-- <a-col :span="8">
                            <a-form-item v-if="queryParam.flid!='31'" label="成果小类：">
                                <a-cascader change-on-select @change="handleCascader" placeholder="请选择成果小类" allowClear :options="options" v-model="xlbh" />
                            </a-form-item>
                            <a-form-item v-if="queryParam.flid=='31'" label="成果小类：">
                                <a-cascader change-on-select @change="handleCascader" placeholder="请选择成果小类" disabled allowClear :options="options" v-model="xlbh" />
                            </a-form-item>
                        </a-col> -->
                        
                        <a-col :span="8">
                            <a-form-item label="所属集团">
                                <a-select v-model="queryParam.ssjtid" placeholder="请选择所属集团">
                                    <a-select-option v-for="group in groupData" :key="group.jttjdwMc" :value="group.id">
                                        {{group.jttjdwMc}}
                                    </a-select-option>
                                </a-select>
                            </a-form-item>
                        </a-col>
                        <a-col :span="8">
                            <a-form-item label="推荐单位">
                                <a-select v-model="queryParam.tjdwid" placeholder="请选择推荐单位">
                                    <a-select-option v-for="item in recommenderData" :key="item.id" :value="item.id">
                                        {{item.jttjdwMc}}
                                    </a-select-option>
                                </a-select>
                            </a-form-item>
                        </a-col>
                        <a-col :span="8">
                            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                                <!--  @click="searchQuery" -->
                                <a-button type="primary" icon="search" @click="searchQuery">查询</a-button>
                                <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                            </span>
                        </a-col>
                    </a-row>
                </a-form>
            </div>
            <div>
                <div style="margin-bottom: 16px;">
                    找到{{waitDataSource.length}}个成果
                </div>
                <a-table
                    ref="table"
                    size="middle"
                    :scroll="{x:true}"
                    bordered
                    rowKey="id"
                    :columns="columns"
                    :dataSource="waitDataSource"
                    :loading="waitLoading"
                    :pagination="ipagination"
                    @change="handleTableChange"
                    class="j-table-force-nowrap"
                    :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
                >
                    <span slot="cxj_xmbh" slot-scope="text, record">
                        <a @click="viewDetail(record)">
                            {{record.cxj_xmbh}}
                        </a>
                    </span>
                </a-table>
            </div>
            <!-- <cxj-jsfs-fp-cg-update :code="code" :flag="flag" :fsfzid="fsfzid1" :fzxmid="fzxmid" @handleUpdateCgInfo="handleUpdateCgInfo"></cxj-jsfs-fp-cg-update> -->
        </j-modal>
    </div>
</template>

<script>
import { getAction } from '@/api/manage';
import {
    cxjsbTjdwlist,
    cxjsbSsjtlist,
    cxjsbTjdwlbList,
    cxjsbTjdwlistBylb,
    sysCxjFlCxjsbList,
    // 删除多个
    cxjfsFzxmdeleteBatch,
    // 成果小类
    cxjsbLoadTreeRoot,
    // 复审分组项目表-答辩时间修改
    cxjfsDbsjEdit,
    // 复审分组项目表-答辩序号修改
    cxjfsDbxhEdit,
    // 复审分组项目表-主审专家修改
    cxjfsZszjEdit
} from "@/api/api"
import Vue from 'vue';
import { ACCESS_TOKEN } from "@/store/mutation-types"
import CxjJsfsFpCgUpdate from "./CxjJsfsFpCgUpdate"
export default {
    props: ['resultFlag','flId','fsfzid'],
    components: {
        CxjJsfsFpCgUpdate
    },
    data(){
        return {
            code: "",
            flag: false,
            fsfzid1: '',
            fzxmid: "",
            selectTotal: 0,
            waitLoading: false,
            visible: false,
            switchFullscreen: true,
            loading: false,
            waitLoading: false,
            url: {
                list: "/cxj/cxjXsscFzXm/cxjxs_fzxmlist_dx",
            },
            selectColumns: [
                {
                    title: '序号',
                    dataIndex: '',
                    key:'rowIndex',
                    width: 60,
                    align: "center",
                    customRender:function (t,r,index) {
                        return parseInt(index)+1;
                    },
                    // fixed: "left",
                },
                {
                    title:'答辩时间',
                    align:"center",
                    dataIndex: 'dbsj',
                    width: 120,
                    scopedSlots: { customRender: 'cxj_dbsj' },
                },
                {
                    title:'答辩序号',
                    align:"center",
                    dataIndex: 'xm_xh',
                    width: 120,
                    scopedSlots: { customRender: 'cxj_xmxh' },
                },
                {
                    title:'主审专家',
                    align:"center",
                    dataIndex: 'zj_xm',
                    width: 120,
                    scopedSlots: { customRender: 'cxj_zjxm' },
                },
                {
                    title:'成果编号',
                    align:"center",
                    dataIndex: 'cxj_xmbh',
                    scopedSlots: { customRender: 'cxj_xmbh' },
                    width: 120
                },
                {
                    title:'成果名称',
                    align:"center",
                    dataIndex: 'cxj_mc_zw',
                    width: 220
                },
                {
                    title:'申报单位',
                    align:"center",
                    dataIndex: 'sbdw_qymc',
                    width: 220
                },
                {
                    title:'成果类别',
                    align:"center",
                    dataIndex: 'fl_mc',
                    width: 100
                },
                // {
                //     title:'成果小类',
                //     align:"center",
                //     dataIndex: 'xlmc',
                //     width: 100
                // },
                {
                    title:'联系人',
                    align:"center",
                    dataIndex: 'lxr_xm',
                    width: 100
                },
                {
                    title:'手机号',
                    align:"center",
                    dataIndex: 'lxr_sj',
                    width: 100
                }
            ],
            // 表头
            columns: [
                {
                    title: '序号',
                    dataIndex: '',
                    key:'rowIndex',
                    width: 60,
                    align: "center",
                    customRender:function (t,r,index) {
                        return parseInt(index)+1;
                    },
                    // fixed: "left",
                },
                {
                    title:'成果编号',
                    align:"center",
                    dataIndex: 'cxj_xmbh',
                    scopedSlots: { customRender: 'cxj_xmbh' },
                    width: 120
                },
                {
                    title:'成果名称',
                    align:"center",
                    dataIndex: 'cxj_mc_zw',
                    width: 220
                },
                {
                    title:'申报单位',
                    align:"center",
                    dataIndex: 'sbdw_qymc',
                    width: 220
                },
                {
                    title:'成果类别',
                    align:"center",
                    dataIndex: 'fl_mc',
                    width: 100
                },
                // {
                //     title:'成果小类',
                //     align:"center",
                //     dataIndex: 'xlmc',
                //     width: 100
                // },
                {
                    title:'联系人',
                    align:"center",
                    dataIndex: 'lxr_xm',
                    width: 100
                },
                {
                    title:'手机号',
                    align:"center",
                    dataIndex: 'lxr_sj',
                    width: 100
                }
            ],
            dataSource: [],
            // 待选成果
            waitDataSource: [],
            // 推荐单位
            recommenderTypeData: [],
            // 所属集团
            groupData: [],
            recommenderData: [],
            selectionRows: [],
            selectionRowsArr: [],
            selectedRowKeys: [],
            selectedRowKeysArr: [],
            queryParam: {
                pageNo: 1,
                pageSize: 10
            },
            /* 排序参数 */
            isorter: {
                column: 'createTime',
                order: 'desc',
            },
            /* 筛选参数 */
            filters: {},
            /* 高级查询条件 */
            superQueryParams: '',
            /* 分页参数 */
            ipagination: {
                current: 1,
                pageSize: 10,
                pageSizeOptions: ['10', '20', '100'],
                showTotal: (total, range) => {
                    return range[0] + "-" + range[1] + " 共" + total + "条"
                },
                showQuickJumper: true,
                showSizeChanger: true,
                total: 0
            },
            cxjSbList: [],
            /* 成果小类参数开始 */
            options: [],
            xlFlag: false,
            loadTreeRoot: [],
            xlbh: [],
            /* 成果小类参数结束 */

        }
    },
    watch: {
        resultFlag(next, prev) {
            if (next != prev) {
                var self = this;
                self.visible = !self.visible;
                self.fsfzid1 = self.fsfzid;
                self.queryParam = {
                    flid: this.flId
                }
                self.selectedRowKeys = [];
                self.selectionRows = [];
                self.getsysCxjFlCxjsbList();
                self.getcxjsbTjdwlist();
                self.getCxjsbTjdwlbList();
                self.getCxjsbSsjtlist();
                self.selectedResultlist();
                self.waitResultList();
                self.getCxjsbLoadTreeRoot();
            }
        }
    },
    methods: {
        // 显示弹框答辩时间、答辩序号、主审专家
        handleUpdate(code, id){
            console.log('====',code)
            /** code
             * code=1，答辩时间
             * code=2, 答辩序号
             * code=3, 主审专家
             */
            this.code = code;
            this.fzxmid = id;
            this.flag = !this.flag;
        },
        // 修改答辩时间、答辩序号、主审专家
        handleUpdateCgInfo(data){
            console.log(data)
            let formData = new FormData();
            formData.append('fzxmid', data.fzxmid)
            if (this.code == 1) {
                formData.append('cxjFssjId', data.cxjFssjId)
                cxjfsDbsjEdit(formData).then(res=>{
                    if (res.success) {
                        this.$message.success(res.message);
                        this.selectedResultlist();
                        this.flag = !this.flag;
                    } else {
                        this.$message.success(res.message);
                    }
                })
            } else if (this.code == 2) {
                formData.append('xh', data.xh)
                cxjfsDbxhEdit(formData).then(res=>{
                    console.log(res)
                    if (res.success) {
                        this.$message.success(res.message);
                        this.selectedResultlist();
                        this.flag = !this.flag;
                    } else {
                        this.$message.success(res.message);
                    }
                })
            } else if (this.code == 3) {
                formData.append('zjid', data.zjid)
                cxjfsZszjEdit(formData).then(res=>{
                    if (res.success) {
                        this.$message.success(res.message);
                        this.selectedResultlist();
                        this.flag = !this.flag;
                    } else {
                        this.$message.success(res.message);
                    }
                })
            }
        },
        handleCancel(){
            this.visible = false;
            this.selectedRowKeys = [];
            this.selectedRowKeysArr = [];
            this.$parent.$parent.loadData();
        },
        onSelectChange(selectedRowKeys, selectionRows) {
            this.selectedRowKeys = selectedRowKeys;
            this.selectionRows = selectionRows;
        },
        // 切换成果类别
        handlCglb(value){
            console.log(value)
            this.queryParam.flid = value;
            this.getCxjsbLoadTreeRoot();
        },
        // 切换成果小类
        handleCascader(value){
            this.queryParam.xlbh = value.join(';')
        },
        // 获取成果小类
        getCxjsbLoadTreeRoot(){
            let data = {
                async: false,
                pcode: this.queryParam.flid
            }
            cxjsbLoadTreeRoot(data).then(res=>{
                let data = res.data;
                if (res.success) {
                    this.xlFlag = true;
                    this.loadTreeRoot = res.result;
                    this.options = this.convertAchievementsTree(res.result);
                } else {
                    this.options = [];
                    if(this.options.length){
                        this.xlFlag = true;
                    } else {
                        this.xlFlag = false;
                    }
                }
            })
        },

        // 重组成果小类
        convertAchievementsTree(tree) {
            var self = this;
            const result = [];
            // 遍历 tree
            tree.forEach(item => {
                // 读取 map 的键值映射
                const value = item.code;
                const label = item.title;
                let children = item.children;
                let children_arr = [];
                if (item.children && item.children.length) {
                    children_arr = self.convertAchievementsTree(item.children);
                }
                // 如果有子节点，递归
                if (children && children.length > 0) {
                    children = self.convertAchievementsTree(children);
                    result.push({
                        value,
                        label,
                        children
                    });
                } else {
                    var data = {
                        value,
                        label
                    };
                    result.push(data);
                }
            });
            return result;
        },
        // 查看成果资料
        viewDetail(item){
            let token = Vue.ls.get(ACCESS_TOKEN);
            sessionStorage.setItem('token', token);
             let routeData = this.$router.resolve({
                path: '/shenbao/declare',
                query: {
                    type: item.fl_id,
                    cxj_cxj_id: item.id,
                    isHead: 2,
                    from: 'gly',
                    cxj_xmbh: item.cxj_xmbh,
                    cxj_mc_zw: item.cxj_mc_zw
                }
            });
            window.open(routeData.href, '_blank');
        },

        getsysCxjFlCxjsbList(){
            sysCxjFlCxjsbList(null).then(res=>{
                if (res.success) {
                    this.cxjSbList = res.result;
                }
            })
        },
        searchQuery(){
            if (!this.queryParam.cxj_xmbh) {
                delete this.queryParam.cxj_xmbh
            }
            if (!this.queryParam.cxj_mc_zw) {
                delete this.queryParam.cxj_mc_zw
            }
            if (!this.queryParam.sbdwmc) {
                delete this.queryParam.sbdwmc
            }
            if (!this.queryParam.cxj_xmbh) {
                delete this.queryParam.cxj_xmbh
            }
            if (!this.queryParam.flid) {
                delete this.queryParam.flid
            }
            if (!this.queryParam.ssjtid) {
                delete this.queryParam.ssjtid
            }
            if (!this.queryParam.tjdwid) {
                delete this.queryParam.tjdwid
            }
            this.waitResultList();
        },
        searchReset() {
            // this.queryParam = {}
            this.xlbh = [];
            this.queryParam = {
                flid: this.flId
            }
            delete this.queryParam.xlbh;
            this.waitResultList();
        },
        onSelectChange(selectedRowKeys, selectionRows) {
            this.selectedRowKeys = selectedRowKeys;
            this.selectionRows = selectionRows;
            // console.log('待选成果',selectedRowKeys)
        },
        onSelectChangeArr(selectedRowKeys, selectionRows){
            this.selectedRowKeysArr = selectedRowKeys;
            this.selectionRowsArr = selectionRows;
            // console.log('已选成果',this.selectedRowKeysArr);
        },
        // 获取已选中的成果
        selectedResultlist(){
            let that = this;
            let httpurl = "/cxj/cxjFsFzXm/cxjfs_fzxmlist_yx"
            let formData = {
                fsfzid: this.fsfzid
            }
            this.loading = true;
            getAction(httpurl,formData).then((res)=>{
                if(res.success){
                    this.dataSource = res.result;
                    this.selectTotal = res.result.length;
                }
                if (res.code === 510) {
                    this.$message.warning(res.message)
                }
                this.loading = false;
            }).finally(() => {
                that.confirmLoading = false;
            })
        },
        // 获取待选成果
        waitResultList(){
            let that = this;
            let httpurl = "/cxj/cxjFsFzXm/cxjfs_fzxmlist_dx"
            that.waitLoading = true;
            // console.log('获取参数',that.queryParam)
            getAction(httpurl,that.queryParam).then((res)=>{
                if(res.success){
                    that.waitDataSource = res.result;
                    if (res.result.total) {
                        that.ipagination.total = res.result.total;
                    } else {
                        that.ipagination.total = 0;
                    }
                }
                if (res.code === 510) {
                    that.$message.warning(res.message)
                }
                that.waitLoading = false;
            }).finally(() => {
                that.confirmLoading = false;
            })
        },
        // 确定
        handleOk(){
            let data = {
                ids: this.selectedRowKeys.join(','),
                fsfzid: this.fsfzid
            }
            console.log('项目是',data)
            if (this.selectedRowKeys.length) {
                this.$emit("addResult",  data)
            } else {
                this.$message.warning("请选择要添加的成果");
            }
            
        },

        // 获取推荐单位类别列表
        getCxjsbTjdwlbList(){
            cxjsbTjdwlbList().then(res=>{
                if (res.success) {
                    this.recommenderTypeData = res.result;
                }
            })
        },
        getcxjsbTjdwlist(){
            cxjsbTjdwlist().then(res=>{
                if (res.success) {
                    this.recommenderData = res.result;
                }
            })

        },
        // 获取所属集团数据
        getCxjsbSsjtlist(){
            cxjsbSsjtlist().then(res=>{
                if (res.success) {
                    this.groupData = res.result;
                }
            })
        },
        // 删除已选成果
        deleteResult(){
            if (this.selectedRowKeysArr.length) {
                let data = {
                    ids: this.selectedRowKeysArr.join(',')
                }
                cxjfsFzxmdeleteBatch(data).then(res=>{
                    if (res.success) {
                        this.selectedRowKeysArr = [];
                        this.$message.success(res.message);
                        this.selectedResultlist();
                        this.waitResultList();
                    }
                })
            } else {
                this.$message.warning("请选择要删除的成果！");
            }
            
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            //分页、排序、筛选变化时触发
            //TODO 筛选
            if (Object.keys(sorter).length > 0) {
                this.isorter.column = sorter.field;
                this.isorter.order = "ascend" == sorter.order ? "asc" : "desc"
            }
            this.ipagination = pagination;
            this.waitResultList();
        },
    }
}
</script>

<style scoped>

</style>